from rest_framework import serializers
from .models import CourseCategory,Course,Teacher,CourseChapter,CourseSection



# 课程分类序列化类
class CourseCategoryModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseCategory
        fields = ['id','name']


# 课程群查序列化类
class TeacherModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Teacher
        fields = ['id','name','role_name','title','signature','image','brief']

class CourseModelSerializer(serializers.ModelSerializer):
    teacher = TeacherModelSerializer()  # 子序列化
    class Meta:
        model = Course
        fields = ['id','name','course_img','course_type_name',
                  'brief','level_name','period','attachment_path',
                  'status_name','students','sections','pub_sections',
                  'price','teacher','course_category','course_section']


# 章节序列化类
class CourseSectionModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseSection
        fields = ['name','orders','section_type','section_link','duration','free_trail']

class CourseChapterModelSerializer(serializers.ModelSerializer):
    # 子序列化方法实现，一个章节下有很多课时，要写many=True
    coursesections = CourseSectionModelSerializer(many=True)
    class Meta:
        model = CourseChapter
        fields = ['course','chapter','name','summary','coursesections']
